import ssl
from qcloudsms_py import SmsMultiSender,SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
from django.conf import settings

def send_sms_single(phone_num,template_id,template_param_list):
    """单条发送短信
        :param  phone_num:手机号
        :param  template_id:腾讯云短信模版ID
        :param  template_param_list:短信模版所需参数列表如:[验证码{1},描述:{2}],则传递参数[888,666]给模版
    """
    # 把下面这三个参数放在settings中定义，并用local_settings中替换为真实的数据
    appid = settings.TENCENT_SMS_APP_ID ## 上面创建的应用的SDK AppID
    appkey = settings.TENCENT_SMS_APP_KEY ## 上面创建的应用的App Key
    sms_sign = settings.TENCENT_SMS_SIGN  ## 步骤1-4中输入的签名内容。使用公众号的话一般为公众号全称或者是简称
    sender = SmsSingleSender(appid ,appkey)
    ## 没有腾讯的账户信息，只能用下面的模拟功能来模拟短信的发送
    # try:
    #     response = sender.send_with_param(86,phone_num,template_id,template_param_list,sign=sms_sign)
    # except HTTPError as e:
    #     response = {'result':1000,'errmsg':'网络异常发送失败'}
    # return response

    ## 模拟发送信息。由于腾讯的账户需要开通，先用模拟信息来模拟收到的验证码功能
    if int(template_param_list[0]) > 6666:
        response = {'result': 0, 'data': template_param_list[0]}
    else:
        response = {'result': 1000, 'errmsg': '网络异常发送失败'}
    return response

def send_sms_multi(phone_num_list,template_id,param_list):
    """批量发送短信
    :param  phone_num_list:手机号列表
    :param  template_id:腾讯云短信模版ID
    :param  param_list:短信模版所需参数列表如:[验证码{1},描述:{2}],则传递参数[888,666]按顺序格式化模版   """
    appid = 1400957966 ## 上面创建的应用的SDK AppID
    appkey = "a85b48896e23dcaa195f09abcb5332f0" ## 上面创建的应用的App Key
    sms_sign = "XXXX"  ## 步骤1-4中输入的签名内容。使用公众号的话一般为公众号全称或者是简称
    sender = SmsSingleSender(appid ,appkey)
    try:
        response = sender.send_with_param(86,phone_num_list,template_id,param_list,sign=sms_sign)
    except HTTPError as e:
        response = {'result':1000,'errmsg':'网络异常发送失败'}
    return response

# if __name__ == '__main__':
# result1 = send_sms_single('15131255089',548769,[666,])
# print(result1)
# result2 = send_sms_single(['15131255089', '15131256780', '15134803089'],548769,[666,])
# print(result2)

















